package com.mgm.exception;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;

public class SMGlobalExceptionHandler implements HandlerExceptionResolver{
	
	private static final Logger logger =LoggerFactory.getLogger(SMGlobalExceptionHandler.class);

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		
		ModelAndView mv = new ModelAndView();
		/*  使用FastJson提供的FastJsonJsonView视图返回，不需要捕获异常   */  
        FastJsonJsonView view = new FastJsonJsonView(); 
		
		Map<String, Object> resMap = new HashMap<String, Object>();  
		resMap.put("code", "0"); 
		resMap.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
		resMap.put("msg", ex.getMessage()); 
		view.setAttributesMap(resMap);
        mv.setView(view); 
        
		logger.debug("[SMGlobalExceptionHandler catch errro]:" + ex.getMessage(), ex);  
		
        return mv; 
	}

}
